<?php
namespace Home\Controller;
use Think\Controller;
/**
 * 购物区域  1现货区(运营商) 2网购区 3店长(运营商) 4乡镇 5 工程
 */
class ShopownerController extends Controller {
  /**
   * 获取左侧分类树
   */
     public function index(){

          $deviceid=!empty($_GET['deviceid'])?$_GET['deviceid'] :'';
          $areatype=!empty($_GET['areatype'])?$_GET['areatype'] :'';////购物区
          if(empty($deviceid)||empty($areatype)){
            $result['status']=0;
           $result['msg']='参数有误';
           echo json_encode($result,JSON_UNESCAPED_UNICODE);
           return '';

          }
          $yid=M('device a')->join('inner join '.C('DB_PREFIX').'shop_user b ON a.`shopid`=b.`shopid`')->getField("b.founder");///查找运营商id
          
          if($areatype==2){

          $where['areatype']=$areatype;

          }else{
            $where['areatype']=$areatype;
            $where['yid']=$yid;
          }
          
          $tids=M('item')->where($where)->distinct('typeid')->getField("typeid",true);
          $typeids=M('item')->where($where)->Field("brandid,typeid")->select();
          $typeids=$this->array_unique_fb($typeids);
          $temp=array();
         foreach ($tids as $key => $value) {
           foreach ($typeids as $k => $v) {
             if ($v[1] ==$value) {
              $temp[$value]['typeid']=$value;
               $temp[$value]['typename']=M('type')->where("typeid='%d'",$value)->getField("typename");
              $temp[$value]['brands'][]=$v[0];

            }
          }
        }
          
      
    /*品牌*/
         $i=0;
        foreach($temp as $k=>&$v){

        foreach($v['brands'] as $key=> $val){

         $v['brands'][$k][$i]['brandid']=M('brand')->where("brandid=%d",$val)->getField("brandid");
         $v['brands'][$k][$i]['brandname']=M('brand')->where("brandid=%d",$val)->getField("brandname");
         if (empty($v['brands'][$k][$i]['brandid']) || empty($v['brands'][$k][$i]['brandname'])  ) 
         {
           unset($v['brands'][$k][$i]);
         }
        

         unset($v['brands'][$key]);
         $i+=1;

        }
        }
     
        foreach ($temp as $key => &$value) {
         
          foreach ($value['brands'] as $k =>&$v) {
            foreach ($v as $i => $val)
             {
              $value['brand'][]=$val;
             }
           unset($v);
          }
          unset($value['brands']);
        }

    

      /**获取顶级分类*/
       $pids=M('type')->where('pid=0')->order("sort")->getField('typeid',true);
       $newtemp=array();
       foreach ($pids as $key => &$value) {
           foreach ($temp as $k => $v) {
             $pid=M('type')->where('typeid=%d',$k)->getField('pid');
             if($value ==$pid){
                $newtemp[$value]['typeid']=$value;
                $newtemp[$value]['typename']=M('type')->where('typeid=%d',$value)->getField('typename');
                 $newtemp[$value]['imgpath']=M('type')->where('typeid=%d',$value)->getField('imgpath');
                $newtemp[$value]['leve2'][]=$v;


             }

           }
           unset($value);
       }

       if(empty($newtemp))
       {
         $result['status']=0;
         $result['msg']='暂无信息';
          
    echo json_encode($result,JSON_UNESCAPED_UNICODE);

       }
       else
       {
          $result['status']=1;
         $result['tree']=array_values($newtemp);
          
    echo json_encode($result,JSON_UNESCAPED_UNICODE);

       }
           
     
    }



      public function array_unique_fb($array2D){
                     foreach ($array2D as $v){
                         $v = join(",",$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
                         $temp[] = $v;

                     }
                     
                     $temp = array_unique($temp);    //去掉重复的字符串,也就是重复的一维数组
                     
                    foreach ($temp as $k =>$v){
                        $temp[$k] = explode(",",$v);   //再将拆开的数组重新组装
                    }
                    return $temp;
                }
       }


